<?php
class Controller_Admin_Ziying extends Controller_Form
{
    public function __construct ($req, $res)
  {
        $this->_list_pagesize = 20;
        $this->_form_type  = 'ziying';
        
        parent::__construct($req, $res);
        $this->_list_where = array(
        array('status', '=', 'VALID'),
        );
  }
    
    public function action_index ()
  {     $this->action_list();  }

    
    public function action_create ()
  {
        $post = $this->request->post();
        if (! empty($post))
        {
        $this->rebuild_post_data($post);
        $this->_form_data = $post;
        }        
        parent::action_create();
  }
  
    public function after_create($nid)
  {
  	    $city=DB::select('city')
  	    ->from('mod_ziying')
  	    ->where('id','=',$nid)
  	    ->limit(1)
  	    ->execute()
  	    ->current()
  	    ->get('city');
  	    if(!empty($city))
  	    {
  	    	$city=explode('|',$city);
  	    }
  	    $this->updateModZiyingCity($city,$nid);
  }
    
    public function action_update ($nid=null)   
  {
       $post = $this->request->post();
       if (! empty($post))
       {
       $this->rebuild_post_data($post);
       $this->_form_data = $post;
       }
       else
       {
       $data = Form_Data::detail($this->_form_type, $nid);
       //web图片处理 
       if(isset($data['images']))
       {$data['images'] = explode('|',$data['images']);}
       
       if(isset($data['descProduct']))
       {$data['descProduct'] = explode('|',$data['descProduct']);}
       
       if(isset($data['descLine']))
       {$data['descLine'] = explode('|',$data['descLine']);}
       
       if(isset($data['descPrice']))
       {$data['descPrice'] = explode('|',$data['descPrice']);}
       
       if(isset($data['descOther']))
       {$data['descOther'] = explode('|',$data['descOther']);}
       //app图片处理
       if(isset($data['descLineApp']))
       {$data['descLineApp'] = explode('|',$data['descLineApp']);}
       
       if(isset($data['descPriceApp']))
       {$data['descPriceApp'] = explode('|',$data['descPriceApp']);}
       //价格处理
       if(isset($data['priceTotal']))
       {$data['priceTotal']=number_format($data['priceTotal']/100,2);}
       //城市处理
       if(isset($data['city']))
       {
       	$data['city']=explode('|',$data['city']);	       
       }
       $this->_form_data = $data;
       }

       parent::action_update($nid);
   }
   
   public function after_update($nid)
   {
   	   $city=DB::select('city')
   	   ->from('mod_ziying')
   	   ->where('id','=',$nid)
   	   ->limit(1)
   	   ->execute()
   	   ->as_array();
   	   if(!empty($city))
   	  {
   		$city=explode('|',$city[0]['city']);
   	  }
   	  $this->updateModZiyingCity($city,$nid);
   }

    public function action_detail ($nid)
   {
       $data = Form_Data::detail($this->_form_type, $nid);
       if(isset($data['priceTotal']))
       {$data['priceTotal']=number_format($data['priceTotal']/100,2);}
       $data['priceTotal'].='元';
       $this->_form_data = $data;
       parent::action_detail($nid);
   }

   public function action_category ($nid)
    {
    	$post = $this->request->post();
    	if (empty($post))
    	{
    		$this->template->set_filename('smarty:www/ziying/admin_category');
                $subcats=App_Category::sub_categories(4);
                $cates=array();
                foreach ($subcats as $id=>$name)
                {
            	  $cates[]= array('name'=>$name,'children'=>App_Category::all_categories(0, 0, FALSE, $id));
                }
    		//$cates = App_Category::all_categories(0, 0, FALSE, 4);
    		$this->template->set('cates', $cates);
    
    		$zy   = new Ziying($nid);
    		$detail= $zy->detail();
    		$this->template->set('ziying', $detail);
    
    		$relation = App_CategoryGL::listByTarget('ZIYING', $nid);
    		$catIds   = array();
    		foreach ($relation as $rs)
    		{
    			$catIds[] = $rs['categoryId'];
    		}
    
    		$this->template->set('catIds', $catIds);
    	}
    	else
    	{
    		$cids = Arr::get($post, 'cid');
    		App_CategoryGL::setTarget(user::current_user_id(), 'ZIYING', $nid, $cids);
    		$this->to_url('list');
    	}
    }

    private function rebuild_post_data (& $post)
   {
   	   //列表缩率图
       if(isset($post['thumb'])){
       foreach($post['thumb'] as $v){
       $file_name = DOCROOT.'/'.$v;
       $image = Image::factory($file_name);
       file_put_contents($file_name,$image->resize('280', '220', Image::NONE)->render());
       }
       }
       //web小窗口展示图450*300                                                                                                     
       if(isset($post['images'])){
       foreach($post['images'] as $v){
       $file_name = DOCROOT.'/'.$v;
       $image = Image::factory($file_name);
       file_put_contents($file_name,$image->resize('525', '350', Image::NONE)->render());
       }
       $post['images'] = implode('|', $post['images']); 
       }
       //app自营信息
       if(isset($post['descLineApp'])){
       	foreach($post['descLineApp'] as $v){
       		$file_name = DOCROOT.'/'.$v;
       		$image = Image::factory($file_name);
       		file_put_contents($file_name,$image->resize('400', '566', Image::NONE)->render());
       	}
       	$post['descLineApp'] = implode('|', $post['descLineApp']);
       } 
       if(isset($post['descPriceApp'])){
       	foreach($post['descPriceApp'] as $v){
       		$file_name = DOCROOT.'/'.$v;
       		$image = Image::factory($file_name);
       		file_put_contents($file_name,$image->resize('400', '566', Image::NONE)->render());
       	}
       	$post['descPriceApp'] = implode('|', $post['descPriceApp']);
       }
       //web自营信息
       if(isset($post['descProduct'])){
       	foreach($post['descProduct'] as $v){
       		$file_name = DOCROOT.'/'.$v;
       		$image = Image::factory($file_name);
       		file_put_contents($file_name,$image->resize('800', '1132', Image::NONE)->render());
       	}
       	$post['descProduct'] = implode('|', $post['descProduct']);
       }
       if(isset($post['descLine'])){
       	foreach($post['descLine'] as $v){
       		$file_name = DOCROOT.'/'.$v;
       		$image = Image::factory($file_name);
       		file_put_contents($file_name,$image->resize('800', '1132', Image::NONE)->render());
       	}
       	$post['descLine'] = implode('|', $post['descLine']);
       }
       if(isset($post['descPrice'])){
       	foreach($post['descPrice'] as $v){
       		$file_name = DOCROOT.'/'.$v;
       		$image = Image::factory($file_name);
       		file_put_contents($file_name,$image->resize('800', '1132', Image::NONE)->render());
       	}
       	$post['descPrice'] = implode('|', $post['descPrice']);
       }
       if(isset($post['descOther'])){
       	foreach($post['descOther'] as $v){
       		$file_name = DOCROOT.'/'.$v;
       		$image = Image::factory($file_name);
       		file_put_contents($file_name,$image->resize('800', '1132', Image::NONE)->render());
       	}
       	$post['descOther'] = implode('|', $post['descOther']);
       }   	
       //横幅图
       if(isset($post['bannerUrl'])){
       foreach($post['bannerUrl'] as $v){
       $file_name = DOCROOT.'/'.$v;
       $image = Image::factory($file_name);
       file_put_contents($file_name,$image->resize('1380', NULL , Image::INVERSE)->render());
       }
       }
       
       if($post['uid']==null)
       {$post['uid'] = user::current_user_id();}
       if($post['priceTotal'])
       {$post['priceTotal']=$post['priceTotal']*100;}
       
       if(isset($post['city'])){
          $post['city'] = implode('|', $post['city']);
       }
   }                                                                                                                                                                                                        
    public function list_get_data ($offset, $limit)
   {
       $ziying = new Ziying; 
       list($count, $ziyings) = $ziying->search(NULL, $offset, $limit);
       $this->_list_total_size = $count;
       return $ziyings;
   }
    
    public function list_filter_data (& $rows)
   {
       foreach ($rows as & $rs)
      {
        $priceTotal = number_format($rs['priceTotal']/100,2);
        $rs['priceTotal'] = $priceTotal.' 元';
        if(isset($rs['city']))
        {
            $rs['city']=explode('|',$rs['city']);
        	$cname=App_City::getNameCnById($rs['city'], 'cityName');
        	$ret='';
        	foreach ($cname as $name)
        	{
        		$ret.=$name.'|';
        	}
        	$rs['city']=rtrim($ret,'|');
        }
      }
   }
   
    public function classname_by_list_row (& $row)
   {}
     
    public function actions_by_list_row (& $row, & $menus)
   {     
       unset($menus[0]);
       array_unshift($menus, '<a target="_blank" href="/ziying/detail/'.$row['id'].'">查看</a>');
       $menus[] = '<a href="/admin/ziying/category/'.$row['id'].'">分类</a>';
   }
   
    public function action_delete ($nid)
   {
       $ziying = new Ziying($nid);
       $ziying->remove();
       $this->to_url('list');
   }
   public function updateModZiyingCity($cityIds,$nid)
   {
   	//清空自营项目之前选择的城市
   	   DB::delete('mod_ziying_city')->where('zyId','=',$nid)->execute();
   	   foreach($cityIds as $id)
   	  {
   		DB::insert('mod_ziying_city')
   	    ->columns (array('cityId','zyId'))
   	    ->values (array($id,$nid))
   	    ->execute();
   	  }     
   }


}



